/**
 * Copyright (c) 2009-2011, chunquedong(YangJiandong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 
 * History:
 *     2010-05-05  Jed Young  Creation
 */
package chunmap.service.gmap;

import java.awt.Image;
import java.util.List;

import chunmap.raster.Tile;
import chunmap.raster.gmap.GoogleMapCode;
import chunmap.util.image.ImageCache;
import chunmap.view.Map;
import chunmap.view.layer.Layer;

class GetTileHelper {
	private GoogleMapCode code=new GoogleMapCode();
	private ImageCache cache=new ImageCache();
	private List<Layer> layers;
	
	public GetTileHelper(List<Layer> layers){
		this.layers=layers;
	}
	
	public Image getImage(int x,int y,int z){
		Tile tile=new Tile();
		tile.x=x;
		tile.y=y;
		tile.z=z;
		Image image=null;
		String name=tile.toString();
		//try get cache
		image=cache.find(name);
		if(image!=null){
			return image;
		}
		
		// renderMap
		Map map;
		map = new Map(GoogleMapCode.tileSize, GoogleMapCode.tileSize);
		map.getLayerCollection().setLayers(layers);
		
		map.getView().setViewEnvelop(code.getEnvelope(tile));
		map.render();
		image = map.getPainter().getData();
		
		cache.save(image, name);
		
		return image;
	}
	
	public List<Layer> getLayers() {
		return layers;
	}
}